/**
 * @Title: AdvertisementServiceImpl
 * @Package cn.com.yiqi.wsc.service.advertisement.impl
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-胥钰淋
 * @date 2017/2/27 11:13
 * @version V1.0
 */
package cn.com.yiqi.wsc.service.advertisement.impl;

import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.dao.advertisement.AdvertisementDao;
import cn.com.yiqi.wsc.dao.advertisement.criteria.AdvertisementCriteria;
import cn.com.yiqi.wsc.dao.advertisement.entity.AdvertisementEntity;
import cn.com.yiqi.wsc.service.advertisement.AdvertisementService;
import cn.com.yiqi.wsc.service.advertisement.dataobject.QueryAdvertisementDO;
import cn.com.yiqi.wsc.service.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *@ClassName: AdvertisementServiceImpl
 * @Description: TODO
 * @author 会员系统组-胥钰淋
 * @date 2017/2/27 11:13
 *
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {

    @Autowired
    private AdvertisementDao advertisementDao;

    @Autowired
    private UserService userService;


    @Override
    public PageInfo<AdvertisementEntity> queryList(QueryAdvertisementDO queryAdvertisementDO) {
        PageHelper.startPage(queryAdvertisementDO.getCurrentPage(), queryAdvertisementDO.getPageSize());
        PageInfo<AdvertisementEntity> pageInfo = new PageInfo<>(advertisementDao.selectList(queryAdvertisementDO.getType().getValue(), queryAdvertisementDO.getStatus().getValue()));
        return pageInfo;
    }

    @Transactional
    @Override
    public Result<Boolean> insert(AdvertisementCriteria advertisement) {
        Result<Boolean> result = new Result<>();
        AdvertisementEntity advertisementEntity = new AdvertisementEntity();

        advertisementEntity.setPicUrl(advertisement.getPicUrl());
        advertisementEntity.setType(advertisement.getType());
        advertisementEntity.setItemId(advertisement.getItemId());
        advertisementEntity.setEnable(0);
        advertisementEntity.setClickCount(0);
        advertisementEntity.setSoldCount(0);
        advertisementEntity.setInsertUserId((long)userService.getCurrentUserId());
        int i = advertisementDao.insert(advertisementEntity);
        if (i > 0) {
            result.setReturnValue(true);
        } else {
            result.setReturnValue(false);
        }
        return result;
    }

    @Transactional
    @Override
    public Result<Boolean> update(AdvertisementEntity advertisementEntity) {
        Result<Boolean> result = new Result<>();
        int i = advertisementDao.update(advertisementEntity);
        if (i > 0) {
            result.setReturnValue(true);
        } else {
            result.setReturnValue(false);
        }
        return result;
    }

    @Transactional
    @Override
    public Result<Boolean> delete(long id) {
        Result<Boolean> result = new Result<>();
        AdvertisementEntity advertisementEntity = new AdvertisementEntity();
        advertisementEntity.setId(id);
        int i = advertisementDao.delete(advertisementEntity);
        if (i > 0) {
            result.setReturnValue(true);
        } else {
            result.setReturnValue(false);
        }
        return result;
    }
}
